//导入mongoose包
const mongoose = require('mongoose')
//导入配置文件
const { DBHOST, DBPORT, DBNAME } = require('../config/config')

//去除低版本mongoose包的不重要提醒
mongoose.set('strictQuery', true)

module.exports = (success, error) => {
    //对函数error设置一个默认值
    if (typeof error === 'undefined') {
        error = () => {
            console.log('连接失败')
        }
    }
    //连接mongodb服务下的数据库
    mongoose.connect(`mongodb://${DBHOST}:${DBPORT}/${DBNAME}`)

    //设置连接数据库时的回调
    // 设置连接成功的回调
    mongoose.connection.once('open', () => {
        success()
    });

    // 设置连接错误的回调
    mongoose.connection.on('error', () => {
        error()
    });
}